package com.oncliedisk.administrator.ui;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.R;
import com.oncliedisk.administrator.model.MyAudio;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/11/25 0025.
 */
public class UploadMusic extends Activity {
    /***
     * 记录选择的条目
     */
    private List<MyAudio> mSelected = new ArrayList<MyAudio>();
    private ListView listview;
    /**
     * 获得到本地所有的bean
     */
    private List<MyAudio> data;
    private BaseAdapter adapter;
    private LinearLayout ll_loading;
    private Button bt_selectall;
    private Button select_folder;
    private ImageView iv_back;
    private Button btn_upload;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_uploadmedia);
        ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
        listview = (ListView) findViewById(R.id.listview);
        bt_selectall = (Button) findViewById(R.id.bt_selectall);
        select_folder = (Button) findViewById(R.id.select_folder);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        btn_upload = (Button) findViewById(R.id.btn_upload);
        fillData();


        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ViewHolder holder = (ViewHolder) view.getTag();
                //如果是未选中的CheckBox,则添加动画
                if (holder.i_select.isChecked()) {
                    holder.i_select.setChecked(false);
                    data.get(position).setChecked(false);
                    mSelected.remove(data.get(position));
                } else {
                    addAnimation(holder.i_select);
                    holder.i_select.setChecked(true);
                    data.get(position).setChecked(true);
                    btn_upload.setEnabled(true);

                    mSelected.add(data.get(position));
                }
                if (mSelected.size() == 0) {
                    btn_upload.setEnabled(false);

                }

            }
        });
        bt_selectall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectAll();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btn_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(UploadMusic.this, "点击了上传", Toast.LENGTH_LONG).show();
            }
        });
        select_folder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(UploadMusic.this, "选择上传到某个文件夹", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void fillData() {
//        data = ReadDataFromContentProvider.readAudio(this);
        if (null == data || data.size() == 0) {
            Toast.makeText(this, "没有扫描到音频文件!", Toast.LENGTH_LONG).show();
            return;
        }

        //加载数据，进度条可见
        ll_loading.setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
//                data = ReadDataFromContentProvider.readAudio(UploadMusic.this);
                if (null == data || data.size() == 0) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //没有数据加载，进度条不可见
                            ll_loading.setVisibility(View.INVISIBLE);
                            Toast.makeText(UploadMusic.this, "没有扫描到视频文件!", Toast.LENGTH_LONG).show();
                        }
                    });
                    return;
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //数据加载完毕，进度条不可见
                        ll_loading.setVisibility(View.INVISIBLE);
                        if (adapter == null) {
                            adapter = new AudioAdapter(UploadMusic.this, data);
                            listview.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        }).start();
    }

    class AudioAdapter extends BaseAdapter {
        private Context context;
        private List<MyAudio> data;


        public AudioAdapter(Context context, List<MyAudio> data) {
            super();
            this.context = context;
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            MyAudio info = data.get(position);
            final ViewHolder holder;
            if (null != convertView) {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            } else {
                view = View.inflate(UploadMusic.this, R.layout.item_uploadmedia, null);
                holder = new ViewHolder();
                holder.i_musicthumb = (ImageView) view.findViewById(R.id.iv_icon);
                holder.i_select = (CheckBox) view.findViewById(R.id.cb_select);
                holder.i_tv = (TextView) view.findViewById(R.id.tv_filename);
                view.setTag(holder);
            }
            holder.i_tv.setText(data.get(position).getTitle());
            holder.i_musicthumb.setImageResource(R.mipmap.music);
            holder.i_select.setChecked(info.isChecked());
            return view;
        }
    }

    class ViewHolder {
        ImageView i_musicthumb;
        TextView i_tv;
        CheckBox i_select;
    }

    /**
     * 给CheckBox加点击动画，利用开源库nineoldandroids设置动画
     *
     * @param view
     */
    private void addAnimation(View view) {
        float[] vaules = new float[]{0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.25f, 1.2f, 1.15f, 1.1f, 1.0f};
        AnimatorSet set = new AnimatorSet();
        set.playTogether(ObjectAnimator.ofFloat(view, "scaleX", vaules),
                ObjectAnimator.ofFloat(view, "scaleY", vaules));
        set.setDuration(150);
        set.start();
    }

    private int getSelectItems() {
        int count = 0;
        if (data != null) {
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).isChecked()) {
                    count++;
                }
            }
        }
        return count;
    }

    @Override
    public void onBackPressed() {
        Toast.makeText(this, "选中 " + getSelectItems() + " 条目", Toast.LENGTH_LONG).show();
        super.onBackPressed();
    }

    public void selectAll() {
        if (data != null) {
            if (mSelected.size() < data.size()) {
                mSelected.clear();
                for (MyAudio info : data) {
                    info.setChecked(true);
                    mSelected.add(info);
                }
                btn_upload.setEnabled(true);
            } else if (mSelected.size() == data.size()) {
                for (MyAudio info : data) {
                    info.setChecked(false);
                }
                mSelected.clear();
                btn_upload.setEnabled(false);
            }
            adapter.notifyDataSetChanged();
        }
    }
}



